package dao;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;

public class AcessoBancoMySql {

    private String login;
    private String password;
    private String driver;
    private String url;
    private Connection connection;
    private static AcessoBancoMySql instance;

    private AcessoBancoMySql() {
        login = "root";
        password = "admin";
        driver = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://localhost:3306/base_tv";
    }

    public static AcessoBancoMySql getInstance() {
        if (instance == null) {
            instance = new AcessoBancoMySql();
        }
        return instance;
    }


    public Connection startConnection() throws Exception {
        try {
            Class.forName(driver);

            connection = DriverManager.getConnection(url, login, password);

        } catch (ClassNotFoundException cnfe) {
            System.err.println("Driver not found\n" + cnfe);

        } catch (SQLException sqle) {
            System.err.println("Error ao criar a cconexão com o banco de dados\n");
            System.err.println("\nError Menssage: " + sqle.getMessage());
            System.err.println("\nError Cod.: " + sqle.getErrorCode());
            System.err.println("\nSQL State: " + sqle.getSQLState());

        } catch (Exception e) {
            System.err.println("Error\n" + e);
            throw e;
        } // catch
        return connection;
    } // startConnection

    public void destroyConnection(){
        try {
            connection.close();
        } catch (SQLException sqle) {
            System.err.println("Error on close database\n" + sqle);
        } //catch
    } //destroyConnection
}
